--[[
--
--  CRT revamped
--
--]]

local DogTagEnabled, DogTag = pcall(AceLibrary, "LibDogTag-2.0")
local CRT_MainFrame = CreateFrame("Frame","CRT_MainFrame",UIParent);

crt = nil;
local initcrt = crt;

crt_version = "v109a";

function crt_Update(elementId, dataSource) -- Update Labels
 local f = getglobal("CRT_"..dataSource[elementId]["elementName"]);
 local FS= getglobal("CRT_"..dataSource[elementId]["elementName"].."_Text");
 FS:SetFont(FS.fontFace,tonumber(FS.fontSize),FS.fontStyle);
 if DogTagEnabled and FS.DogTagged == true or FS.DogTagged == "true" or FS.DogTagged == 1 then
  FS:SetText(DogTag:Evaluate(string.lower(FS.relative),FS.string));
 else
  FS:SetText(crt_parser(FS.string));
 end
 if FS.shadowColor ~= nil then
  FS:SetShadowColor(FS.shadowColor["R"],FS.shadowColor["G"],FS.shadowColor["B"],FS.shadowColor["A"]);
 end
 if FS.shadowOffset ~= nil then
  FS:SetShadowOffset(FS.shadowOffset["X"],FS.shadowOffset["Y"]);
 end
 if f.Width ~= nil then
  f:SetWidth(f.Width)
 else
  f:SetWidth(FS:GetStringWidth());
 end
 if f.Height ~= nil then
  f:SetHeight(f.Height)
 else
  f:SetHeight(FS.fontSize);
 end
 f:SetFrameStrata(tostring(f.frameStrata))
 f:SetFrameLevel(math.floor(f.frameLevel))
 f:ClearAllPoints();
 f:SetPoint(f.point["AnchorTo"],getglobal(f.point["RelativeTo"]),f.point["RelativePoint"],tonumber(f.point["OffsetX"]), tonumber(f.point["OffsetY"]));
 if f.moving ~= true then -- do not override drag method
  if f.clickable == true then
   f:EnableMouse(true);
  else
   f:EnableMouse(false);
  end
 end
 FS:SetAllPoints(f);
 f:SetParent(f.point["RelativeTo"])
end

function crt_RequestUpdate(elementId, dataSource) -- Update Labels with new Data from Array
if getglobal("CRT_"..dataSource[tonumber(elementId)]["elementName"]) ~= nil then -- Update initiate
   f               = getglobal("CRT_"..dataSource[tonumber(elementId)]["elementName"]);
   FS              = getglobal("CRT_"..dataSource[tonumber(elementId)]["elementName"].."_Text");
   
   FS.fontFace     = dataSource[tonumber(elementId)]["elementTypeData"].fontFace;
   FS.fontSize     = dataSource[tonumber(elementId)]["elementTypeData"].fontSize;
   FS.fontStyle    = dataSource[tonumber(elementId)]["elementTypeData"].fontStyle;
   FS.DogTagged    = dataSource[tonumber(elementId)]["elementTypeData"].IsDogTagged;
   FS.relative     = string.lower(dataSource[tonumber(elementId)].elementRelative);
   FS.string       = dataSource[tonumber(elementId)]["elementTypeData"].value;
   FS.shadowColor  = dataSource[tonumber(elementId)]["elementTypeData"].shadowColor;
   FS.shadowOffset = dataSource[tonumber(elementId)]["elementTypeData"].shadowOffset;
   if dataSource[tonumber(elementId)].elementClickable == "true" then
    f.clickable     = true
   else
    f.clickable     = false
   end
   f.frameStrata   = dataSource[tonumber(elementId)]["elementJustification"].frameStrata;
   f.frameLevel    = dataSource[tonumber(elementId)]["elementJustification"].frameLevel;
   f.Width         = dataSource[tonumber(elementId)].Width;
   f.Height        = dataSource[tonumber(elementId)].Height;
   f.point         = dataSource[tonumber(elementId)]["elementJustification"].point;
   f:Show();
   if dataSource[tonumber(elementId)]["elementScripts"] ~= nil then
    for index,value in pairs(dataSource[tonumber(elementId)]["elementScripts"]) do
     f:SetScript(index, RunScript(value));
    end
   end 
 end
return false;
end

function crt_Builder(elementId, dataSource) -- Build Labels up
 if getglobal("CRT_"..dataSource[elementId]["elementName"]) == nil then -- element does not exists
   local f,FS;
    f              = CreateFrame("Button","CRT_"..dataSource[elementId]["elementName"]); 
   FS              = f:CreateFontString("CRT_"..dataSource[elementId]["elementName"].."_Text","OVERLAY");
   FS.fontFace     = dataSource[elementId]["elementTypeData"].fontFace;
   FS.fontSize     = dataSource[elementId]["elementTypeData"].fontSize;
   FS.fontStyle    = dataSource[elementId]["elementTypeData"].fontStyle;
   FS.DogTagged    = dataSource[elementId]["elementTypeData"].IsDogTagged;
   FS.relative     = string.lower(dataSource[elementId].elementRelative);
   FS.string       = dataSource[elementId]["elementTypeData"].value;
   FS.shadowColor  = dataSource[elementId]["elementTypeData"].shadowColor;
   FS.shadowOffset = dataSource[elementId]["elementTypeData"].shadowOffset;
   
   f.clickable     = dataSource[elementId].elementClickable;
   f.frameStrata   = dataSource[elementId]["elementJustification"].frameStrata;
   f.frameLevel    = dataSource[elementId]["elementJustification"].frameLevel;
   f.Width         = dataSource[elementId].Width;
   f.Height        = dataSource[elementId].Height;
   f.point         = dataSource[elementId]["elementJustification"].point;
   if dataSource[elementId]["elementScripts"] ~= nil then
    for index,value in pairs(dataSource[elementId]["elementScripts"]) do
     f:SetScript(index, loadstring(value));
    end
   end
 end
end

function crt_OnEvent()
  if initcrt == crt then 
  crt = crt_default;
 crt_activeProfile = "DEFAULT";
  end
end

CRT_MainFrame:RegisterEvent("ADDON_LOADED",crt_OnEvent());
